package org.ishequ.miduo.weibo;

import java.util.ArrayList;
import java.util.List;

import org.ishequ.miduo.MiDuoActivity.ConfigHelper;
import org.ishequ.weibo.adapater.FriendsOrFollowsListAdapter;
import org.ishequ.weibo.domain.UserInfo;
import org.ishequ.weibo.utils.InfoHelper;
import org.ishequ.widget.AutoGetMoreListView;
import org.ishequ.widget.WaitingView;
import weibo4android.User;
import weibo4android.UserWapper;
import weibo4android.Weibo;
import weibo4android.WeiboException;
import weibo4android.org.json.JSONObject;
import android.app.Activity;
import android.app.NotificationManager;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Toast;

public class WeiboFriendsOrFollowsListActivity extends BaseActivity {

	private FriendsOrFollowsListAdapter adapter;
	private AutoGetMoreListView autoGetMoreListView;
	private List<User> list = new ArrayList<User>();
	private List<JSONObject> list_relations = new ArrayList<JSONObject>();
	private boolean flag; // true for follower, false for friends
	private String cid;
	private UserWapper curUserWapper;
	private Weibo weibo;
	
	private Handler handler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			switch (msg.what) {
				case InfoHelper.LOADING_DATA_FAILED:
					Toast.makeText(WeiboFriendsOrFollowsListActivity.this, "获取信息失败",
							Toast.LENGTH_LONG).show();
					WeiboFriendsOrFollowsListActivity.this.finish();
					break;
				case InfoHelper.LOADING_DATA_COMPLETED:
					adapter.notifyDataSetChanged();
					WeiboFriendsOrFollowsListActivity.this
							.setContentView(autoGetMoreListView);
//					Toast.makeText(FriendsOrFollowsListActivity.this, "刷新完成",
//							Toast.LENGTH_LONG).show();
					break;
			}
		}
	};

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		System.setProperty("weibo4j.oauth.consumerKey", Weibo.CONSUMER_KEY);
		System.setProperty("weibo4j.oauth.consumerSecret",Weibo.CONSUMER_SECRET);
		
		weibo = new Weibo();
		weibo.setToken(ConfigHelper.nowUser.getToken(), ConfigHelper.nowUser.getTokenSecret());
		
		this.setContentView(new WaitingView(this));
		
		cid = null;
		Bundle bundle = getIntent().getExtras();
		if (bundle != null) {
			flag = bundle.getBoolean("flag");
			cid = bundle.getString("cid");
		}
		
		if (TextUtils.isEmpty(cid))
			cid = weibo.getUserId() + "";

		autoGetMoreListView = new AutoGetMoreListView(this);
		autoGetMoreListView.setLayoutParams(new LayoutParams(
				LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
		adapter = new FriendsOrFollowsListAdapter(WeiboFriendsOrFollowsListActivity.this, list, autoGetMoreListView);
		autoGetMoreListView.setAdapter(adapter);
		autoGetMoreListView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Intent intent = new Intent(WeiboFriendsOrFollowsListActivity.this,
						UserInfo.class);
				if (position < list.size())
					intent.putExtra("cid", list.get(position - 1).getId() + "");
				startActivity(intent);
			}
		});
		autoGetMoreListView.setOnRefreshListener(adapter);
		autoGetMoreListView.setOnGetMoreListener(adapter);
		// setContentView(autoGetMoreListView);
		new Thread(new Runnable() {
			@Override
			public void run() {
				try {
					if (flag) {
						// list = weibo.getFollowersStatuses(cid);
						curUserWapper = weibo.getFollowersStatuses(cid, -1l);
						list = curUserWapper.getUsers();
						NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
						notificationManager.cancel(WeiboHomeActivity.UNREAD_FOLLOWER);
						weibo.resetCount(4);
					} else {
						// list = weibo.getFriendsStatuses(cid);
						curUserWapper = weibo.getFriendsStatuses(cid, -1l);
						list = curUserWapper.getUsers();
					}
					adapter.setCurUserWapper(curUserWapper);
					adapter.setList(list);
					getRelation(false, 0);
					handler.sendEmptyMessage(InfoHelper.LOADING_DATA_COMPLETED);
				} catch (WeiboException e) {
					e.printStackTrace();
					handler.sendEmptyMessage(InfoHelper.LOADING_DATA_FAILED);
				}
			}
		}).start();
	}

	protected void getRelation(boolean isGetMore, int beg) {
		if (!isGetMore)
			list_relations.clear();

		// boolean followed, following;
		for (int index = beg; index < list.size(); index++) {
			User user = list.get(index);
			// String text = "";
			try {
				// String sex = (user.getGender().equals("m") ? "他" : "她");
				list_relations.add(weibo.showFriendships(weibo.verifyCredentials().getId()
						+ "", user.getId() + ""));
				adapter.setList_relations(list_relations);
				// JSONObject source = object.getJSONObject("source");
				// JSONObject target = object.getJSONObject("target");
				// following = Boolean.valueOf(target.getString("followed_by"));
				// // 我是否关注他
				// followed = Boolean.valueOf(source.getString("followed_by"));
				// // 他是否关注我
				// if (following && followed) {
				// text = text + "\n" + "互相关注";
				// } else {
				// if (following) {
				// text = text + "\n" + "我已关注" + sex;
				// } else {
				// text = text + "\n" + "我未关注" + sex;
				// }
				// if (followed) {
				// text = text + "\n" + sex + "已关注我";
				// } else {
				// text = text + "\n" + sex + "未关注我";
				// }
				// }
			} catch (WeiboException e) {
				e.printStackTrace();
				// } catch (JSONException e) {
				// e.printStackTrace();
			}
		}
	}
	
}
